package com.tusuo.stds.ui.module;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.tusuo.stds.Constants;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.ModuleList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.integrity.IntegrityFragment_;
import com.tusuo.stds.ui.main.TabEntity;
import com.tusuo.stds.ui.rank.RankActivity;
import com.tusuo.stds.ui.rank.RankActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;
import rx.functions.Action0;

/**
 * @author J.Huang
 * @version 1.0
 *
 */

@EActivity(R.layout.activity_module)
public class ModuleActivity extends BaseToolbarActivity<ModuleView, ModulePresenter>
                                implements ModuleView {

    @ViewById(R.id.module_tv_no_levels)
    TextView tvNoLevels;

    @ViewById(R.id.module_tab_layout)
    CommonTabLayout mTabLayout;
    @ViewById(R.id.module_layout_content)
    FrameLayout layoutContent;

    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private ModuleList.Module mModule;

    @AfterInject
    void init() {
        mModule = Parcels.unwrap(getIntent().getParcelableExtra(ModuleList.Module.EXTRA_NAME));
    }

    @AfterViews
    void initData() {
        setupUI(this);

        String toolbarTitle = mModule.Title;

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.MenuAction menuAction = new ToolbarOwner.MenuAction("排行榜", new Action0() {
            @Override
            public void call() {
                Intent intent = new Intent();
                intent.setClass(ModuleActivity.this, RankActivity_.class);
                intent.putExtra("type", RankActivity.RANK_TYPE_MODULE);
                intent.putExtra("id", mModule.Id);
                startActivity(intent);
            }
        }, 0);

        menuActionList.add(menuAction);

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        tvNoLevels.setVisibility(View.GONE);
        mTabLayout.setVisibility(View.VISIBLE);
        layoutContent.setVisibility(View.VISIBLE);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public ModulePresenter createPresenter() {
        return new ModulePresenter(this, mModule.Id);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadData();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);  //这一句必须的，否则Intent无法获得最新的数据
    }

    @Override
    public void showNoLevelsTips() {
        tvNoLevels.setVisibility(View.VISIBLE);
        mTabLayout.setVisibility(View.GONE);
        layoutContent.setVisibility(View.GONE);
    }

    @Override
    public void setListViewAdapter(LevelListAdapter mainLevelAdapter, LevelListAdapter bonusLevelAdapter) {

        if (mFragmentList.size() > 0) {
            // 更新TabLayout中的Fragment
            MainLevelListFragment_ mainFragment = (MainLevelListFragment_) mFragmentList.get(0);
            mainFragment.init(mainLevelAdapter);
            mainFragment.initData();

            if (mModule.CompleteType == 2) {
                IntegrityFragment_ integrityFragment = (IntegrityFragment_) mFragmentList.get(1);
                integrityFragment.initData();
            } else {
                BonusLevelListFragment bonusFragment = (BonusLevelListFragment) mFragmentList.get(1);
                bonusFragment.init(bonusLevelAdapter);
                bonusFragment.initData();
            }
        } else {
            MainLevelListFragment mainFragment = MainLevelListFragment_.builder().build();
            mainFragment.init(mainLevelAdapter);
            mFragmentList.add(mainFragment);

            String[] mTitles;

            if (mModule.CompleteType == 2) {
                mTitles = new String[]{"主线任务", "知识完整度"};
                mFragmentList.add(IntegrityFragment_.builder().mDataId(mModule.Id).mType(Constants.CODE_COMPLETE_TYPE_MODULE).build());
            } else {
                mTitles = new String[]{"主线任务", "奖励关卡"};
                BonusLevelListFragment bonusFragment = BonusLevelListFragment_.builder().build();
                bonusFragment.init(bonusLevelAdapter);
                mFragmentList.add(bonusFragment);
            }

            //设置tab的标题、选中图标、未选中图标
            for (int i = 0; i < mTitles.length; i++) {
                mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
            }

            mTabLayout.setTabData(mTabEntities, this, R.id.module_layout_content, mFragmentList);
        }
    }
}
